package com.springboot.repository;

import com.springboot.domain.BtBank;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BankRepository extends JpaRepository<BtBank, String> {
    @Modifying
    @Query("update BtBank bb set bb.status = '4' where bb.id = ?1")
    void logout(String userId);

    Page<BtBank> findByStatus(String status, Pageable pageable);

    @Query(value = "select new BtBank(b.id,b.name,b.type,b.license,b.principal,b.contact,b.tel,b.email,b.province,b.address,b.establishTime,b.createTime,b.status,b.introduction) from BtBank b where b.status in('2', '4')",
            countQuery = "select count(b.id) from BtBank b where b.status in('2', '4')")
    Page<BtBank> findModifiedByStatus(Pageable pageable);

    @Modifying
    @Query("update BtBank bb set bb.views = ?1 where bb.id = ?2 ")
    void addViews(int total, String id);

    @Query("select bb from BtBank bb where bb.name like concat('%',?1,'%') and bb.status = '2'")
    List<BtBank> findBank(String name);
}
